﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Loki.Commands;

namespace Loki.UI.Wpf
{
    public class ViewControl : UserControl, IView
    {
        private CommandManager _Commands;

        public ViewControl()
        {
            this.Loaded += ViewWindow_Loaded;

            this.DataContextChanged += ViewWindow_DataContextChanged;

            _Commands = new CommandManager();
        }

        void ViewWindow_Closing(object sender, CancelEventArgs e)
        {
            _IsClosing = true;
            if (ViewModel != null)
            {
                ViewModel.Close.Execute(null);
            }
        }

        void ViewWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (ViewModel != null)
            {
                _Commands.Handle(ViewModel.Close, ViewModel_CanClose, ViewModel_Close);
            }

            ViewModel.Initialize();
        }

        protected virtual void ViewModel_CanClose(object sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute = !_IsClosing;
        }

        private bool _IsClosing = false;

        protected virtual void ViewModel_Close(object sender, CommandEventArgs e)
        {

        }

        void ViewWindow_Deactivated(object sender, EventArgs e)
        {
            ViewModel.Desactivate.Execute(null);
        }

        void ViewWindow_Activated(object sender, EventArgs e)
        {
            ViewModel.Activate.Execute(null);
        }

        void ViewWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ViewModel != null)
            {
                ViewModel.LoadData();
            }
        }

        public IViewModel ViewModel
        {
            get
            {
                return DataContext as IViewModel;
            }
            set
            {
                DataContext = value;
            }
        }

        #region Disposed
        public event EventHandler Disposed;

        protected virtual void OnDisposed(EventArgs e)
        {
            EventHandler L_Handler = Disposed;

            if (L_Handler != null)
            {
                L_Handler(this, e);
            }
        }

        #endregion

        public void Show()
        {

        }

        /// <summary>
        /// Releases all resources used by an instance of the <see cref="ViewControl" /> class.
        /// </summary>
        /// <remarks>
        /// This method calls the virtual <see cref="Dispose(bool)" /> method, passing in <strong>true</strong>, and then suppresses 
        /// finalization of the instance.
        /// </remarks>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged resources before an instance of the <see cref="ViewControl" /> class is reclaimed by garbage collection.
        /// </summary>
        /// <remarks>
        /// This method releases unmanaged resources by calling the virtual <see cref="Dispose(bool)" /> method, passing in <strong>false</strong>.
        /// </remarks>
        ~ViewControl()
        {
            Dispose(false);
        }

        /// <summary>
        /// Releases the unmanaged resources used by an instance of the <see cref="ViewControl" /> class and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><strong>true</strong> to release both managed and unmanaged resources; <strong>false</strong> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {

        }
    }
}
